import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

import staticData from './staticData'
const goods = {
  state: {
    goodsData: [],
    /* 商品的 arr */
    goodsInCar: [],
    /* 数量大于 0 的放在购物车篮子里 */
    totalMoney: 0,
    /* 合计总钱数 */
    orderWaitPay: {},
    /* 待付款的订单 */
    detailsData: {} /* 详细订单 点订单查看详情 */
  },
  getters: {



  },
  mutations: {
    setData(state, _arr) {
      // console.log(_arr)
      state.goodsData = _arr
    },
    //增加
    addData(state, _id) {
      console.log(_id);
      let _index;
      state.goodsData.map((item, index) => {
        if (item.id == _id) {
          _index = index
        }
      })
      state.goodsData[_index].num++
    },
    // 递减
    minusData(state, _id) {
      let _index;
      state.goodsData.map((item, index) => {
        if (item.id == _id) {
          _index = index
        }
      })
      state.goodsData[_index].num--
    },
    // 结算
    fixedData(state) {
      let tempArr = [];
      state.goodsData.map(item => {
        if (item.num > 0) {
          tempArr.unshift(item);
        }
      })
      state.goodsInCar = tempArr;
      let tempTotalMoney = 0
      tempArr.map(item => {
        tempTotalMoney += (item.price * item.num)
      })
      state.totalMoney = tempTotalMoney.toFixed(2);
    },
    // 清空购物车
    cleanBuyCar(state) {
      state.goodsInCar = [];
      state.totalMoney = 0;
    },
    // 生成待付订单  传送给 页面
    makeOrderWaitPay(state, _obj) {
      // console.log(_obj)
      state.orderWaitPay = _obj
    },
    // 详细订单 点订单查看详情
    makeOrderDetails(state, _obj) {
      console.log("goods------------>",_obj)
      state.detailsData = _obj
    }
  },
  actions: {
    // 根据 设备id 获取其 餐品 数据 前端发送一个设备id，后端传回该设备内所有食品
    async myGetData(context, _obj) {

      //服务器拿数据
      let products;
      //   products = await axios.post(staticData.doMain + "/getMainImgs", {
      //      id: _obj.id /* 设备id */
      //   }).then(body => {
      //     console.log("body data--->", body.data);
      //     console.log("body data--->", body.data.length);
      //     return body.data
      //   })
      //本地测试
      products = [{
          id: 12, //商品id
          pic: "http://huyaimg.msstatic.com/avatar/1093/f5/9f51df52143c14ba7fab3b01849dd4_180_135.jpg?0?424782", //商品图片
          inventory: "12", //商品库存
          name: "煎饼111", //商品名字
          intro: "简介：这是好吃的韭菜馅饼，选料考究sadasdsadsadasdasdas，入口爽快。", //商品简介
          price: 10, //商品价格
          num: 0 //商品id
        },
        {
          id: 155,
          pic: "http://huyaimg.msstatic.com/avatar/1093/f5/9f51df52143c14ba7fab3b01849dd4_180_135.jpg?0?424782",
          inventory: "14",
          name: "煎饼222222",
          intro: "简介：这是好吃的韭菜馅饼，选料考究，入口爽快。",
          price: 20,
          num: 0
        }
      ]
      context.commit("setData", products);
      return products
    },
    myChangeData(context, _arr) {
      console.log(_arr)
      context.commit("setData", _arr)
    }
  }
}
export default goods;
